package org.dfl.plugin.dialogs;

import java.util.Collections;
import java.util.List;

import org.dfl.core.model.transitions.MultipleInputsTransition;
import org.dfl.messages.Messages;
import org.dfl.plugin.editor.model.ConnectionModelElement;
import org.dfl.plugin.editor.model.TransitionModelElement;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * dialog to change transition input edge properties
 * @author Piotr Wlodarczyk
 *
 */
public class InputEdgePropertiesDialog extends EdgePropertiesDialog {
	private List<String> freeLabels;
	private boolean fixedLabels;
	private String label;
	private String labelOrig;
	private Text labelTextField;
	
	public InputEdgePropertiesDialog(ConnectionModelElement connection, List<String> freeLabels, String label, boolean fixedLabels) {
		super(connection);
		this.connection = connection;
		this.freeLabels = freeLabels;
		this.fixedLabels = fixedLabels;
		this.label = label;
		this.labelOrig = connection.getLabel();
	}

	@Override
	protected Shell getNewShell() {
		Shell shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

		GridLayout dialogShellLayout = new GridLayout();
		dialogShellLayout.makeColumnsEqualWidth = true;
		shell.setLayout(dialogShellLayout);
		shell.layout();
		shell.pack();
		shell.setSize(321, 119);
		shell.setText(Messages.EdgePropertiesDialog_edgeProp);
		
		return shell;
	}

	@Override
	protected void init(final Shell dialogShell) {
		Group selectGroup = new Group(dialogShell, SWT.NONE);
		GridLayout selectGroupLayout = new GridLayout();
		selectGroupLayout.makeColumnsEqualWidth = true;
		selectGroup.setLayout(selectGroupLayout);
		GridData selectGroupLData = new GridData();
		selectGroupLData.grabExcessHorizontalSpace = true;
		selectGroupLData.horizontalAlignment = GridData.FILL;
		selectGroupLData.heightHint = 36;
		selectGroup.setLayoutData(selectGroupLData);
		selectGroup.setText(Messages.TransitionInputSelectionDialog_selectInput);
		if (!fixedLabels) {
			labelTextField = new Text(selectGroup, SWT.NONE);
			GridData inputSelectLData = new GridData();
			inputSelectLData.heightHint = 21;
			inputSelectLData.grabExcessHorizontalSpace = true;
			inputSelectLData.horizontalAlignment = GridData.FILL;
			labelTextField.setLayoutData(inputSelectLData);
			labelTextField.setText(label);
			addEnterKeyListener(labelTextField);
		} else {
			final Combo labelSelect = new Combo(selectGroup, SWT.READ_ONLY);
			GridData inputSelectLData = new GridData();
			inputSelectLData.heightHint = 21;
			inputSelectLData.grabExcessHorizontalSpace = true;
			inputSelectLData.horizontalAlignment = GridData.FILL;
			labelSelect.setLayoutData(inputSelectLData);
			addEnterKeyListener(labelSelect);
			
			Collections.sort(freeLabels);
			for (String key : freeLabels) {
				labelSelect.add(key);
			}
			labelSelect.setText(label);
			labelSelect.addSelectionListener(new SelectionListener() {
				public void widgetDefaultSelected(SelectionEvent arg0) {
					setLabel(freeLabels.iterator().next());
				}

				public void widgetSelected(SelectionEvent arg0) {
					setLabel(labelSelect.getText());
				}
			});
		}
		super.init(dialogShell);
	}

	public void setLabel(String label) {
		this.label = label;
		if(fixedLabels){
			connection.disconnect();
			MultipleInputsTransition t = (MultipleInputsTransition) ((TransitionModelElement)connection.getTarget()).getTransition();
			if(!t.getInputsDefinition().get(labelOrig).getType().canMerge(t.getInputsDefinition().get(label).getType())){
				connection.setNoneProperty();
				//TODO add info that properties where reseted
			}
			connection.setLabel(label);
			connection.reconnect(true);
			resetPropertiesCombo();
		}else{
			connection.disconnect();
			connection.setLabel(label);
			connection.reconnect(true);
		}
	}
	
	@Override
	protected void finish() {
		if(!fixedLabels) setLabel(labelTextField.getText()); // otherwise the label should be already defined
		super.finish();
	}
}
